package com.gingko.duominuo.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.gingko.duominuo.data.model.TaskFilterVo
import com.gingko.duominuo.data.model.Value
import com.gingko.duominuo.databinding.FragmentTaskFilterBinding
import com.gingko.duominuo.databinding.LayoutTaskFilterItemBinding
import com.gingko.duominuo.ui.adapter.TaskFilterAdapter
import com.gingko.duominuo.utils.singleClick
import com.gingko.duominuo.viewmodel.BaseViewModel


class TaskFilterFragment : BaseFragment() {
    private val mViewBinding: FragmentTaskFilterBinding by lazy { FragmentTaskFilterBinding.inflate(layoutInflater) }
    var callback: Callback? = null
    private val adapters = mutableListOf<TaskFilterAdapter>()

    companion object {

        @JvmStatic
        fun newInstance() = TaskFilterFragment()
    }

    override fun getContentView(inflater: LayoutInflater, container: ViewGroup?): View {
        return mViewBinding.root
    }

    override fun getViewModel(): BaseViewModel? {
        return null
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        mViewBinding.tvReset.singleClick {
            adapters.forEach {
                it.selectPos = null
                it.notifyDataSetChanged()
            }
            callback?.onResetFilter()
        }
        mViewBinding.tvConfirm.singleClick {
            callback?.onConfirmFilter(adapters[0].getSelectFilter(), adapters[1].getSelectFilter(), adapters[2].getSelectFilter())
        }
    }

    fun updateData(data: TaskFilterVo) {
        mViewBinding.llFilterContent.removeAllViews()
        data.forEachIndexed { index, taskFilterVoItem ->
            val itemBinding = LayoutTaskFilterItemBinding.inflate(layoutInflater, mViewBinding.llFilterContent, false)
            itemBinding.tvProject.text = taskFilterVoItem.name
            val adapter = TaskFilterAdapter(requireContext(), taskFilterVoItem.values ?: listOf())
            itemBinding.rcvProject.adapter = adapter
            adapters.add(adapter)
            itemBinding.viewLine1.isVisible = index != data.size - 1
            mViewBinding.llFilterContent.addView(itemBinding.root)
        }
    }

    interface Callback {
        fun onConfirmFilter(project: Value?, platform: Value?, taskType: Value?)
        fun onResetFilter()
    }
}